package com.enterprise.controller.api.enterprise;

import com.alibaba.fastjson.JSON;
import com.enterprise.controller.api.common.BaseResponse;
import com.enterprise.controller.api.enterprise.vo.EnterpriseResp;
import com.enterprise.entity.Enterprise;
import com.enterprise.service.EnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequestMapping("/api/enterprise")
@Controller("apiEnterpriseController")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @ResponseBody
    @RequestMapping("/queryEnterpriseList")
    public BaseResponse<List<EnterpriseResp>> queryCarList(){
        BaseResponse response = new BaseResponse();
        List<Enterprise> enterpriseList = enterpriseService.selectList(new Enterprise());
        List<EnterpriseResp> enterpriseResps = new ArrayList<>();
        for (Enterprise enterprise:enterpriseList) {
            EnterpriseResp enterpriseResp = new EnterpriseResp();
            BeanUtils.copyProperties(enterprise,enterpriseResp);
            enterpriseResps.add(enterpriseResp);
        }
        log.info("queryEnterpriseList enterpriseResps: {}" , JSON.toJSONString(enterpriseResps));
        response.setData(enterpriseResps);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryEnterpriseView/{id}")
    public BaseResponse<EnterpriseResp> queryEnterpriseView(@PathVariable("id") String id){
        BaseResponse response = new BaseResponse();
        Enterprise enterprise = enterpriseService.selectById(Integer.valueOf(id));
        EnterpriseResp enterpriseResp = new EnterpriseResp();
        BeanUtils.copyProperties(enterprise,enterpriseResp);
        log.info("queryEnterpriseView enterpriseResp: {}" , JSON.toJSONString(enterpriseResp));
        response.setData(enterpriseResp);
        return response;
    }


}
